package com.carlinfo.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.common.util.ConstantFinalUtil;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.dao.IDynastyDao;
import com.carlinfo.dao.IKingDao;
import com.carlinfo.dao.impl.DynastyDaoImpl;
import com.carlinfo.dao.impl.KingDaoImpl;
import com.carlinfo.pojo.ADynasty;
import com.carlinfo.pojo.AKing;
import com.carlinfo.service.IDemoService;


public class DemoServiceImpl implements IDemoService
{
	private IDynastyDao dynastyDao = new DynastyDaoImpl();
	private IKingDao kingDao = new KingDaoImpl();
	/**
	 * 将 json对象转换成字符串
	 * */
	@Test
	public JSONObject saveOneDynastyService(ADynasty dynasty)
	{
		JSONObject resultJSON = new JSONObject();
		/*	调用dao保存	*/
		int res = this.dynastyDao.saveOneDao(dynasty);
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("0"));
			
			/*	将data 类型 转换成	JSONObject*/
			JSONObject dataJSON = new JSONObject();
			dataJSON .put("effect",res);
			dataJSON.put("id", dynasty.getId());
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneDynastyService(ADynasty dynasty)
	{
		JSONObject resultJSON = new JSONObject();
		/*	调用dao更新	*/
		int res = this.dynastyDao.updateOneDao(dynasty);
		/*	当res大于零的时候  就证明更新	*/
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("0"));
			
			/*	将data 类型 转换成	JSONObject*/
			JSONObject dataJSON = new JSONObject();
			dataJSON .put("effect",res);
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneDynastyService(Map<String, Object> condMap)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.dynastyDao.deleteOneDao(condMap);
		/*	当res大于零的时候  就证明删除成功了	*/
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("0"));
			
			/*	将data 类型 转换成	JSONObject*/
			JSONObject dataJSON = new JSONObject();
			dataJSON .put("effect",res);
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public ADynasty selectOneDynastyService(Map<String, Object> condMap)
	{
		return this.dynastyDao.selectOneDao(condMap);
	}

	@Override
	public List<ADynasty> selectListDynastyService(PageInfoUtil pageInfoUtil ,Map<String, Object> condMap)
	{
		return this.dynastyDao.selectListDao(pageInfoUtil,condMap);
	}
	/*----------------皇上操作开始-------------------------------*/
	@Override
	public JSONObject saveOneKingService(AKing king)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.kingDao.saveOneDao(king);
		/*	当res大于零的时候  就证明删除成功了	*/
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("0"));
			
			/*	将data 类型 转换成	JSONObject*/
			JSONObject dataJSON = new JSONObject();
			dataJSON .put("effect",res);
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneKingService(AKing king)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.kingDao.updateOneDao(king);
		/*	当res大于零的时候  就证明删除成功了	*/
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("0"));
			
			/*	将data 类型 转换成	JSONObject*/
			JSONObject dataJSON = new JSONObject();
			dataJSON .put("effect",res);
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneKingService(Map<String, Object> condMap)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.kingDao.deleteOneDao(condMap);
		/*	当res大于零的时候  就证明删除成功了	*/
		if(res>0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("0"));
			
			/*	将data 类型 转换成	JSONObject*/
			JSONObject dataJSON = new JSONObject();
			dataJSON .put("effect",res);
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public AKing selectOneKingService(Map<String, Object> condMap)
	{
		AKing king = this.kingDao.selectOneDao(condMap);
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 专门用来查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			/* 先清理掉 */
			extMap.clear();
			extMap.put("id", king.getDynastyId());
			ADynasty dynasty = this.selectOneDynastyService(extMap);
			king.setDynasty(dynasty);
		}
		return king ; 
	}

	@Override
	public List<AKing> selectListKingService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AKing> kingList = this.kingDao.selectListDao(pageInfoUtil,condMap);
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 专门用来查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			for (Iterator iterator = kingList.iterator(); iterator.hasNext();)
			{
				AKing king = (AKing) iterator.next();
				/* 先清理掉 */
				extMap.clear();
				extMap.put("id", king.getDynastyId());
				ADynasty dynasty = this.selectOneDynastyService(extMap);
				king.setDynasty(dynasty);
			}
		}
		return kingList ; 
	}

}
